package com.sskj.spk.http;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import com.sskj.common.base.App;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.StockCoinBean;
import com.sskj.lib.util.APKVersionCodeUtils;
import com.sskj.spk.bean.AppVersionBean;
import com.sskj.spk.bean.ChartViewCj;
import com.sskj.spk.bean.LevelVolumeBean;
import com.sskj.spk.bean.MainBannerBean;
import com.sskj.spk.bean.NoticeList;
import com.sskj.spk.bean.SocketBean;
import com.sskj.spk.bean.SocketSendBean;
import com.sskj.spk.bean.SummaryBean;

import java.util.List;

import okhttp3.WebSocket;


/**
 * 作者 :吕志豪
 * 简书：https://www.jianshu.com/u/6e525b929aac
 * github：https://github.com/lvzhihao100
 * 描述：
 * 创建时间：2018-10-15 14:08
 */
public class HttpService implements IHttpService {

    @Override
    public GetRequest<HttpData<MainBannerBean>> getBannerImage() {
        return OkGo.<HttpData<MainBannerBean>>get(HttpConfig.BASE_URL + HttpConfig.MAIN_BANNER);
    }

    @Override
    public PostRequest<HttpData<PageBean<NoticeList>>> getGGList(int number) {
        return OkGo.<HttpData<PageBean<NoticeList>>>post(HttpConfig.BASE_URL + HttpConfig.NOTICE);
    }

    @Override
    public GetRequest<HttpData<ChartViewCj>> getLevelVolumeList(String code, int page) {
        return OkGo.<HttpData<ChartViewCj>>get(HttpConfig.BASE_URL + HttpConfig.TRADE_LEVEL_VOLUME)
                .params("stockCode", code)
                .params("page", page)
                .params("pageSize", 20);
    }

    @Override
    public GetRequest<HttpData<List<LevelVolumeBean>>> getLevelVolumeListBb(String code, int page) {
        return OkGo.<HttpData<List<LevelVolumeBean>>>get(HttpConfig.BASE_URL + HttpConfig.TRADE_LEVEL_VOLUME_BB)
                .params("code", code)
                .params("page", page)
                .params("pageSize", 20);
    }

    @Override
    public GetRequest<HttpData<List<StockCoinBean>>> getProduct(String code) {
        return OkGo.<HttpData<List<StockCoinBean>>>get(HttpConfig.BASE_URL + HttpConfig.PRODUCT_LIST)
                //                .params("codeType", code.contains("/") ? null : code)
//                .params("code", code.contains("/") ? code : null)
                .params("codeType", code.contains("/") ? code.split("/")[1] : code)
                .params("code", code.contains("/") ? code : null);
    }

    @Override
    public WebSocketObserver pushCoinType(String type) {
        return new WebSocketObserver(HttpConfig.WS_STOCK) {
            @Override
            public void onOpen(WebSocket webSocket, okhttp3.Response response, String msg) {
                if (TextUtils.isEmpty(msg)) {
                    webSocket.send(new Gson().toJson(new SocketSendBean(type)));
                } else {
                    webSocket.send(msg);
                }
            }
        };
    }

    @Override
    public WebSocketObserver pushCoinFive(String type) {
        return new WebSocketObserver(HttpConfig.WS_STOCK_PANKOU) {
            @Override
            public void onOpen(WebSocket webSocket, okhttp3.Response response, String msg) {
                if (TextUtils.isEmpty(msg)) {
                    webSocket.send(new Gson().toJson(new SocketSendBean(type)));
                } else {
                    webSocket.send(msg);
                }
            }
        };
    }

    @Override
    public PostRequest<HttpData<AppVersionBean>> getVersion() {
        return OkGo.<HttpData<AppVersionBean>>post(HttpConfig.BASE_URL + HttpConfig.GET_VERSION)
                .params("version", APKVersionCodeUtils.getVerName(App.INSTANCE))
                .params("type", 1);

    }

    @Override
    public GetRequest<HttpData<SummaryBean>> getSummary(String code) {
        return OkGo.<HttpData<SummaryBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_MUMMARY)
                .params("pid", code);


    }

    @Override
    public GetRequest<HttpData<SocketBean>> getSocket() {
        return OkGo.<HttpData<SocketBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_SOCKET);
    }
}